//
//    User.swift
//
//    Create by 徐挺 陈 on 24/3/2018
//    Copyright © 2018. All rights reserved.
//    Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import SwiftyJSON
import PromiseKit


class User: NSObject, NSCoding {

    var token: String!
    var userId: Int!
    var userName: String!
    var phone: String!
    var password: String!
    var recordId: Int!

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    override init() {
        super.init()
        phone = ""
        password = ""
    }

    init(fromJson json: JSON!) {
        if json.isEmpty {
            return
        }
        token = json["token"].stringValue
        userId = json["userId"].intValue
        userName = json["userName"].stringValue
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String: Any]
    {
        var dictionary = [String: Any]()
        if token != nil {
            dictionary["token"] = token
        }
        if userId != nil {
            dictionary["userId"] = userId
        }
        if userName != nil {
            dictionary["userName"] = userName
        }
        return dictionary
    }

    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        token = aDecoder.decodeObject(forKey: "token") as? String
        userId = aDecoder.decodeObject(forKey: "userId") as? Int
        userName = aDecoder.decodeObject(forKey: "userName") as? String
        phone = aDecoder.decodeObject(forKey: "phone") as? String
        password = aDecoder.decodeObject(forKey: "password") as? String
    }

    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    func encode(with aCoder: NSCoder)
    {
        if token != nil {
            aCoder.encode(token, forKey: "token")
        }
        if userId != nil {
            aCoder.encode(userId, forKey: "userId")
        }
        if userName != nil {
            aCoder.encode(userName, forKey: "userName")
        }
        if phone != nil {
            aCoder.encode(phone, forKey: "phone")
        }
        if password != nil {
            aCoder.encode(password, forKey: "password")
        }

    }

    func login() -> Promise<JSON> {
        let dic: [String: Any] = [
            "phone": phone!,
            "password": password!,
        ]
        let dataFn = { [weak self] (json: JSON) in
            self?.userId = json["userId"].intValue
            self?.userName = json["userName"].stringValue
            self?.token = json["token"].stringValue
        }
        let request = RequestData(path: "User/UserLogin", param: dic)
        return netRequest(request, dataFn: dataFn)
    }

    func register(_ code: String) -> Promise<JSON> {
        let dic: [String: Any] = [
            "phone": phone!,
            "password": password!,
            "code": code,
        ]
        let dataFn = { [weak self] (json: JSON) in
            self?.userId = json["userId"].intValue
            self?.userName = json["userName"].stringValue
            self?.token = json["token"].stringValue
        }
        let request = RequestData(path: "User/UserRegister", param: dic)
        return netRequest(request, dataFn: dataFn)
    }

    func getVerificateCode() -> Promise<JSON> {
        let dic: [String: Any] = [
            "phone": phone!,
        ]
        let dataFn = { [weak self] (json: JSON) -> Void in
            self?.recordId = json["recordId"].intValue
        }
        let request = RequestData(path: "User/GetVerificateCode", param: dic)
        return netRequest(request, dataFn: dataFn)
    }

    func getLoanInformation() -> Promise<LoanInformation> {
        var loanInformation: LoanInformation!
        let dataFn = {  (json: JSON) -> Void in
            loanInformation = LoanInformation(fromJson: json)
        }
        let request = RequestData(path: "User/GetUserAllInfo")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: loanInformation)
        }
    }
    
    func getBankInfo() -> Promise<BankInfo> {
        var bankInfo: BankInfo!
        let dataFn = { (json: JSON) -> Void in
            bankInfo = BankInfo(fromJson: json)
        }
        let request = RequestData(path: "User/GetUserBankInfo")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: bankInfo)
        }
    }
}
